<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="doAddRloes">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getBasicUserInfo } from '@/api/user'
import { addRolesPermission } from '@/api/permissions'
export default {
  props: {
    curEmployeesId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: []
    }
  },
  created() {
    this.loadRoles()
    this.loadAssignRoles()
  },
  methods: {
    closeDialog() {
      this.$emit('closeDialog')
    },
    async loadRoles() {
      const res = await getRoles()
      this.roleList = res.data.rows
    },
    async loadAssignRoles() {
      const res = await getBasicUserInfo(this.curEmployeesId)
      this.roleIds = res.data.roleIds
    },
    async doAddRloes() {
      await addRolesPermission({ id: this.curEmployeesId, roleIds: this.roleIds })
      this.$message.success('分配角色成功')
      this.closeDialog()
    }
  }
}
</script>
